﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using RibbonEditor.Rules;

namespace RibbonEditor.Controls.Rules
{
    public partial class TabDisplayRuleControl : UserControl, IControlRulable
    {
        TabDisplayRule rule;

        internal TabDisplayRule Rule
        {
            get { return this.rule; }
            set { this.rule = value; }
        }

        public TabDisplayRuleControl(TabDisplayRule rule)
        {
            InitializeComponent();
            this.rule = rule;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                if (this.stringControl1.Visible)
                {
                    this.stringControl1.Value = this.rule.GetTabCommand();
                }

                if (this.rcId.Visible)
                {
                    this.rcId.Value = this.rule.GetTabCommand();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (this.stringControl1.Value.Length > 0)
                {
                    this.rule.SetTabCommand(this.stringControl1.Value);
                }
                else if (this.rcId.Value.Length > 0)
                {
                    this.rule.SetTabCommand(this.rcId.Value);
                }

                UserControlHelper.CleanDirtyFields(this);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildObjectXml(XmlNode parentNode)
        {
            try
            {
                ((ITabDisplayRule)this.rule).AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
